# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.


from odoo import api, fields, models, tools, _

import math

FAPIAO_LINE_NAME_MAX = 28
FAPIAO_LINE_SPEC_MAX = 28


class SaleOrderLineMixin(models.AbstractModel):
    _name = 'fp.sale_line.mixin'
    _description = "Sale Line Mixin"

    product_id = fields.Many2one(
        'fp.product', string='Product', required=True, index=True)

    vat_product_id = fields.Many2one(
        string='税收商品ID', related='product_id.vat_product_id', store=True)

    vat_product_code = fields.Char(
        string='Var Product code', related='product_id.vat_product_code', store=True)
    vat_product_name = fields.Char(
        string='Var Product Name', related='product_id.vat_product_name', store=True)
    product_code = fields.Char(
        string='Product code', related='product_id.code', store=True)
    product_name = fields.Char(
        string='Product Name', related='product_id.name', store=True)
    product_spec = fields.Char(
        string='Spec', related='product_id.spec', store=True)
    product_uom = fields.Char(
        string='Uom', related='product_id.uom', store=True)

    fapiao_line_count = fields.Integer(
        string='Product Len', compute='_compute_fapiao_line_count', store=True)

    tax_ratio = fields.Float(string='tax_ratio', required=True, default=0.0)
    quantity = fields.Float(string='Quantity', required=True, default=1.0)
    price_unit = fields.Float('Unit Price', required=True, default=0.0)

    amount_total = fields.Float(
        string='Total', compute='_compute_amount', store=True)
    amount_untaxed = fields.Float(
        string='Untaxed Amount', compute='_compute_amount', store=True)
    amount_tax = fields.Float(
        string='Tax', compute='_compute_amount', store=True)

    # ods.sale.order 没有 discount
    amount_discount = fields.Float(string='Discount Amount',  default=0.0)
    amount_untaxed_discount = fields.Float(
        string='Discount Untaxed Amount', compute='_compute_discount', store=True)
    amount_tax_discount = fields.Float(
        string='Discount Tax', compute='_compute_discount', store=True)

    @api.depends('amount_discount')
    def _compute_discount(self):
        for line in self:
            untaxed = line.amount_discount / (1 + (line.tax_ratio/100))
            untaxed = round(untaxed, 2)
            tax = line.amount_discount - untaxed
            tax = round(tax, 2)

            line.amount_untaxed_discount = untaxed
            line.amount_tax_discount = tax

    @api.depends('quantity', 'price_unit')
    def _compute_amount(self):
        for line in self:
            amount_total = line.price_unit * line.quantity
            amount_total = round(amount_total, 2)
            amount_untaxed = amount_total / (1 + (line.tax_ratio/100))
            amount_untaxed = round(amount_untaxed, 2)
            amount_tax = amount_total - amount_untaxed
            amount_tax = round(amount_tax, 2)

            line.amount_total = amount_total
            line.amount_untaxed = amount_untaxed
            line.amount_tax = amount_tax

    @api.depends('vat_product_name', 'product_name', 'product_spec')
    def _compute_fapiao_line_count(self):
        for line in self:
            vat_product_name = line.vat_product_name or ''
            product_name = line.product_name or ''

            name1 = '*' + vat_product_name + '*' + product_name
            spec = line.product_spec or ''
            length1 = len(name1.encode('gbk'))
            length2 = len(spec.encode('gbk'))
            count1 = math.ceil(length1/FAPIAO_LINE_NAME_MAX)
            count2 = math.ceil(length2/FAPIAO_LINE_SPEC_MAX)
            count = count1 > count2 and count1 or count2
            self.fapiao_line_count = count
